home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_300 / 310_02 / sstr.c < prev    next >
C/C++ Source or Header  |  1990-04-18  |  4KB  |  194 lines

  1. /*
  2.     sstr - find and replace string occurrences
  3.         with common addresses,
  4.         can be used to share strings accross compiled boundaries
  5.         written by tim budd, 9/84
  6. */
  7. /*
  8.     The source code for the Little Smalltalk System may be freely
  9.     copied provided that the source of all files is acknowledged
  10.     and that this condition is copied with each file.
  11.  
  12.     The Little Smalltalk System is distributed without responsibility
  13.     for the performance of the program and without any guarantee of
  14.     maintenance.
  15.  
  16.     All questions concerning Little Smalltalk should be addressed to:
  17.  
  18.         Professor Tim Budd
  19.         Department of Computer Science
  20.         Oregon State University
  21.         Corvallis, Oregon
  22.         97331
  23.         USA
  24. */
  25. # include <stdio.h>
  26. # define WORDTABMAX 1000
  27. # define STRTABMAX 10000
  28.  
  29. int x_cmax = 0;
  30. int x_tmax = -1;
  31. char x_str[STRTABMAX];
  32. char *x_tab[WORDTABMAX];
  33.  
  34.  
  35. main(argc, argv)
  36. int argc;
  37. char **argv;
  38. {    int i;
  39.     FILE *fd;
  40.  
  41.     if (strcmp(argv[1], "-f") == 0) {
  42.         for (i = 2; i < argc; i++) {
  43.             fd = fopen(argv[i], "r");
  44.             if (fd != NULL) {
  45.                 findstrs(fd);
  46.                 fclose(fd);
  47.                 }
  48.             }
  49.         }
  50.     else if (strcmp(argv[1], "-t") == 0) {
  51.         for (i = 4; i < argc; i++)
  52.            puts(argv[i]);
  53.         fd = fopen(argv[2], "r");
  54.         if (fd == NULL) {
  55.             fprintf(stderr,"can't open string table\n");
  56.             exit(1);
  57.             }
  58.         maketab(fd, stdout, argv[3]);
  59.         }
  60.     else {
  61.         fd = fopen(argv[1], "r");
  62.         if (fd == NULL) {
  63.             fprintf(stderr,"can't open string table\n");
  64.             exit(1);
  65.             }
  66.         maketab(fd, 0, 0);
  67.         printf("extern char x_str[];\n");
  68.         replacestr(stdin);
  69.         }
  70.     exit(0);
  71. }
  72.  
  73. /* findstrs - find all strings and output them to stdout */
  74. findstrs(fd)
  75. FILE *fd;
  76. {
  77.     char *p, buffer[500];
  78.     int c;
  79.  
  80.     for (; (c = getc(fd)) != EOF; )
  81.         if (c == '\"') {
  82.             for (p = buffer; (c = getc(fd)) != '\"'; p++)
  83.                 if (c == EOF) {
  84.                     fprintf(stderr,"unexpected eof\n");
  85.                     exit(1);
  86.                     }
  87.                 else *p = c;
  88.             *p = '\0';
  89.             puts(buffer);
  90.             }
  91. }
  92.  
  93. /* replacestr - replace strings with their address in x_str */
  94. replacestr(fd)
  95. FILE *fd;
  96. {
  97.     char *p, buffer[500], *w_search();
  98.     int c;
  99.  
  100.     for (; (c = getc(fd)) != EOF; )
  101.         if (c != '\"') putchar(c);
  102.         else {
  103.             for (p = buffer; (c = getc(fd)) != '\"'; p++)
  104.                 if (c == EOF) {
  105.                     fprintf(stderr,"unexpected eof\n");
  106.                     exit(1);
  107.                     }
  108.                 else *p = c;
  109.             *p = '\0';
  110.             p = w_search(buffer, 0);
  111.             if (p) printf("&x_str[%d]", p - &x_str[0]);
  112.             else printf("\"%s\"", buffer);
  113.             }
  114. }
  115.  
  116. maketab(ifd, ofd, itab)
  117. FILE *ifd, *ofd;
  118. char *itab;
  119. {    char wbuf[100], *p;
  120.     int i;
  121.  
  122.     x_cmax = 0;
  123.     if (ofd)
  124.         fprintf(ofd, "char x_str[] = {");
  125.     while (fgets(wbuf, 100, ifd) != NULL) {
  126.         x_tab[++x_tmax] = &x_str[x_cmax];
  127.         for (p = wbuf; *p; p++) {
  128.             if (*p == '\n') {*p = '\0'; break;}
  129.             if (ofd)
  130.                 fprintf(ofd,"0%o, ", *p);
  131.             x_str[x_cmax++] = *p;
  132.             }
  133.         if (ofd)
  134.             fprintf(ofd, "0,   /* %s */\n", wbuf);
  135.         x_str[x_cmax++] = '\0';
  136.         }
  137.     if (ofd) {
  138.         fprintf(ofd, "0 };\n");
  139.         fprintf(ofd, "int x_cmax = %d;\n", x_cmax);
  140.         }
  141.     if (itab) {
  142.         fprintf(ofd, "static symbol x_sytab[] = {\n");
  143.         for (i = 0; i <= x_tmax; i++) {
  144.             fprintf(ofd, "{1, SYMBOLSIZE, &x_str[%d]}, /* ", 
  145.                 x_tab[i]-x_tab[0]);
  146.             for (p = x_tab[i]; *p; p++) 
  147.                 putc(*p, ofd);
  148.             fprintf(ofd," */\n");
  149.             }
  150.         fprintf(ofd, "0};\n");
  151.         fprintf(ofd, "symbol *x_tab[%s] = {\n", itab);
  152.         for (i = 0; i <= x_tmax; i++) {
  153.             fprintf(ofd, "&x_sytab[%d], /* ",i); 
  154.             for (p = x_tab[i]; *p; p++) 
  155.                 putc(*p, ofd);
  156.             fprintf(ofd," */\n");
  157.             }
  158.         fprintf(ofd, "0};\n");
  159.         fprintf(ofd,"int x_tmax = %d;\n", x_tmax);
  160.         }
  161. }
  162.  
  163. /*     
  164.     word search for table routines
  165. */
  166.  
  167. char *w_search(word, insert)
  168. char *word;
  169. int  insert;
  170. {    int i,j,k;
  171.  
  172.     for (i=1; i <= x_tmax; i <<= 1);
  173.     for (i >>= 1, j = i >>1, i--; ; j >>= 1) {
  174.         if (! (k = strcmp(word, x_tab[i])))
  175.             return(x_tab[i]);
  176.  
  177.         if (!j) break;
  178.         if (k < 0) i -= j;
  179.         else {
  180.             if ((i += j) > x_tmax) i = x_tmax;
  181.             }
  182.         }
  183.     if (insert) {
  184.         for (k = ++x_tmax; k > i; k--) {
  185.             x_tab[k] = x_tab[k-1];
  186.             }
  187.         if (!(x_tab[i] = (char *) malloc(1 + strlen(word))))
  188.             return((char *) 0);
  189.         strcpy(x_tab[i], word);
  190.         return(x_tab[i]);
  191.         }
  192.     else return((char *) 0);
  193. }
  194.